package auctionhaus



import grails.test.mixin.*
import org.junit.*
import org.grails.datastore.mapping.model.types.Simple

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Bid)
@Mock([Customer, Listing])
class BidTests {
    def listing
    def seller
    def bidder
    def startBid

    // setup objects for unit testing
    public void setUp() {
        mockForConstraintsTests(Bid)

        listing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 5,
                seller: seller,
                startBid: 1
        )

        seller = new Customer(
                email: "johndoe@acme.com",
                password: "1234567"
        )

        bidder = new Customer(
                email: "janeDoe@acme.com",
                password: "1234567"
        )
    }

    // test to make sure the amount is valid
    void testAmountConstraints() {
        def badBid = new Bid(
                bidder: bidder,
                listing: listing
        )
        assert !badBid.validate()

        def goodBid = new Bid(
                amount: 2,
                bidder: bidder,
                listing: listing
        )
        assert goodBid.validate()
    }

    // make the bid is in a valid format
    void testTimeConstraints() {
        def newBid = new Bid(
                amount: 2,
                bidder: bidder,
                listing: listing
        )
        assert newBid.validate()
    }

    // make sure the bid's bidder is in a valid format
    void testBidderConstraints() {
        def newBid = new Bid(
                amount: 2,
                listing: listing
        )
        assert !newBid.validate()

        newBid.bidder = bidder;
        assert newBid.validate()
    }

    // test to make sure the listing is in the correct version
    void testListingConstraints() {
        def newBid = new Bid(
                amount: 2,
                bidder: bidder
        )
        assert newBid.validate()

        newBid.listing = listing
        assert  newBid.validate()
    }

    // clean up after unit tests
    public void tearDown() {
        listing = null
        seller = null
        bidder = null
    }
}
